package com.itboye.jinpuyiyuan.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.bean.PhysicalListBean;
import com.itboye.jinpuyiyuan.bean.TestServerFixedBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2017/12/13 0013.
 */

public class TestServerFixedAdapter extends BaseAdapter {
    private ArrayList<PhysicalListBean.PhysicalBean> list;
    private Context context;
    private LayoutInflater inflater;
    public  TestServerFixedAdapter(Context context,ArrayList<PhysicalListBean.PhysicalBean> list){
        this.list=list;
        this.context=context;
        inflater=LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView==null){
            myViewHolder=new MyViewHolder();
            convertView=inflater.inflate(R.layout.item_testserver_fixedlist,parent,false);
            myViewHolder.title=convertView.findViewById(R.id.title);
            myViewHolder.strength=convertView.findViewById(R.id.strength);
            myViewHolder.newPrice=convertView.findViewById(R.id.newPrice);
            myViewHolder.oldPrice=convertView.findViewById(R.id.oldPrice);

            myViewHolder.logoImg=convertView.findViewById(R.id.logoImg);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder= (MyViewHolder) convertView.getTag();
        }
        myViewHolder.title.setText(list.get(position).getName());
        if(list.get(position).getSex().trim().equals("0")){ //通用
            myViewHolder.logoImg.setImageResource(R.drawable.nn);
        }else if (list.get(position).getSex().trim().equals("1")){//男性
            myViewHolder.logoImg.setImageResource(R.drawable.man);
        }else if(list.get(position).getSex().trim().equals("2")){//女性
            myViewHolder.logoImg.setImageResource(R.drawable.woman);
        }
        String departments="";
        SimpleDateFormat format=new SimpleDateFormat("yyyy-M-dd hh:mm:ss");

        if (!"".equals(list.get(position).getDiscountBeginDate())){
            Date start,end;
            try {
                start= format.parse(list.get(position).getDiscountBeginDate());
                end= format.parse(list.get(position).getDiscountEndDate());
                if (System.currentTimeMillis()>=start.getTime()&&System.currentTimeMillis()<=end.getTime()){
                    myViewHolder.newPrice.setText("¥"+list.get(position).getDiscountPrice());
                    myViewHolder.oldPrice.setText("原价¥"+list.get(position).getPrice());
                    myViewHolder.oldPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG); //中划线
                }else {
                    myViewHolder.newPrice.setText("¥"+list.get(position).getPrice());
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else {
                myViewHolder.newPrice.setText("¥"+list.get(position).getPrice());
        }


        for (int i=0;i<list.get(position).getItem_list().size();i++){
            if (i==list.get(position).getItem_list().size()-1){
                departments+=list.get(position).getItem_list().get(i).getDeptname()+"("+
                        list.get(position).getItem_list().get(i).getItemname()+")";
            }else {
                departments+=list.get(position).getItem_list().get(i).getDeptname()+"("+
                        list.get(position).getItem_list().get(i).getItemname()+"),";
            }
        }
        myViewHolder.strength.setText(departments);


//        setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG); // 设置中划线并加清晰
//        Glide.with(context).load(list.get(position).getLogoUrl()).into(myViewHolder.logoImg);
        return convertView;
    }

    public void addData(ArrayList<PhysicalListBean.PhysicalBean> list){
        this.list=list;
        this.notifyDataSetChanged();
    }
    class MyViewHolder{
        TextView title,strength,newPrice,oldPrice;
        ImageView logoImg;
    }
}
